<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>系统分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索查询 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <template>
            <el-select v-model="queryParam.type" placeholder="请选择类别">
              <el-option v-for="item in optionTypeDict" :key="item.value" :label="item.name" :value="item.value">
              </el-option>
            </el-select>
          </template>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="getOptionList">查询</el-button>
          <el-button type="primary" @click="optionFormVisible = true">添加系统分类</el-button>
        </el-col>
      </el-row>
      <!-- table表格数据 start -->
      <template>
        <el-table :data="optionDataList" style="width: 100%" max-height="570">
          <!-- 数据表 -->
          <el-table-column fixed prop="name" label="名称" width="100" >
          </el-table-column>
          <el-table-column prop="sort" label="排序" width="50">
          </el-table-column>
          <el-table-column prop="type_text" label="类别" width="120">
          </el-table-column>
          <el-table-column prop="icon" label="图标" width="80">
            <template slot-scope="scope">
              <span class="icon1 iconfont" :class="scope.row.icon"></span>
            </template>
          </el-table-column>
          <el-table-column prop="enable" label="在用" width="80">
            <template slot-scope="scope">
              <el-switch @change="enableChange(scope.row)" v-model="scope.row.enable" active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="notes" label="备注" width="150">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <!-- 操作 -->
            <template slot-scope="scope">
              <!-- <el-button size="mini" type="warning" icon="el-icon-view" circle></el-button> -->
              <el-button size="mini" type="warning" icon="el-icon-edit" @click="editOption(scope.row)" circle></el-button>
              <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteOption(scope.$index,scope.row.id)"
                circle></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- table表格数据 end -->
      <!-- 分页  start-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryParam.currentPage"
        :page-sizes="[10, 20, 30, 40]" :page-size="100" layout="total, sizes, prev, pager, next, jumper" :total="totalResult">
      </el-pagination>
      <!-- 分页  end> -->
    </el-card>
    <!-- 表单 start -->
    <el-dialog title="创建/修改系统分类" :visible.sync="optionFormVisible" width="30%" center @close="closeOptionForm">
      <el-form :model="optionForm" status-icon :rules="optionFormRules" ref="optionFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="类别" prop="type">
          <el-select v-model="optionForm.type" placeholder="请选择活动区域">
            <el-option v-for="item in optionTypeDict" :key="item.value" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="optionForm.name"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="optionForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否在用" prop="enable">
          <el-switch v-model="optionForm.enable"></el-switch>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" v-model.number="optionForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input type="textarea" v-model="optionForm.notes"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetOptionForm">重置</el-button>
        <el-button type="primary" @click="updateOrCreateOptionForm">确认</el-button>
      </span>
    </el-dialog>
    <!-- 表单 end -->
  </div>
</template>

<script>
  import {
    _getSystemOptionDictApi,
    _getSystemOptionListApi,
    _deleteSystemOptionApi,
    _updateOrCreateSystemOptionApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        operate: 'add',
        optionDataList: [],
        totalResult: 0,
        optionTypeDict: [],
        queryParam: {
          type: '',
          currentPage: 1,
          pageSize: 10
        },
        optionFormVisible: false,
        optionForm: {},
        optionFormTemp: {
          type: '',
          name: '',
          icon: '',
          sort: 0,
          notes: '',
          enable: true
        },
        optionFormRules: {
          name: [{
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }],
          type: [{
            required: true,
            message: '请选择类型',
            trigger: 'change'
          }]
        }
      }
    },
    created() {
      this.optionForm = JSON.parse(JSON.stringify(this.optionFormTemp))
      this.getOptionList()
      this.getOptionTypeData()
    },
    methods: {
      // 获取列表数据
      async getOptionList() {
        // 获取分类列表
        const {
          data: res
        } = await _getSystemOptionListApi(this.queryParam)
        console.log(res)
        this.optionDataList = res.data.dataList
        this.totalResult = res.data.totalResult
      },
      async getOptionTypeData() {
        // 获取所有类型的数据
        const {
          data: res
        } = await _getSystemOptionDictApi()
        console.log(res)
        this.optionTypeDict = res.data.option_type
      },
      async deleteOption(index, id) {
        // 删除
        const data = await _deleteSystemOptionApi(id)
        if (data.status === 200 && data.data.status) {
          this.$message.success('删除成功！')
          this.optionDataList.splice(index, 1)
        }
      },
      updateOrCreateOptionForm() {
        this.$refs.optionFormRef.validate(async valid => {
          if (valid) {
            const {
              data: res
            } = await _updateOrCreateSystemOptionApi(this.optionForm, this.operate)
            console.log(res)
            if (res.status) {
              var message = this.operate === 'add' ? '添加成功！' : '编辑成功！'
              this.$message.success(message)
              this.optionFormVisible = false
              this.operate = 'add'
              this.getOptionList()
            } else {
              this.$message.error('操作失败！')
            }
          }
        })
      },
      editOption(option) {
        this.operate = 'edit'
        this.optionForm = option
        this.optionFormVisible = true
      },
      async enableChange(option) {
        // 状态更新
        const {
          data: res
        } = await this.$http.put('system/option/' + option.id + '/', option)
        if (res.status) {
          this.$message.success('修改成功！')
        } else {
          this.$message.error('操作失败！')
        }
      },
      // 分页事件
      handleSizeChange(newSize) {
        this.queryParam.pageSize = newSize
        this.queryParam.currentPage = 1
        this.getOptionList()
      },
      handleCurrentChange(newPage) {
        this.queryParam.currentPage = newPage
        this.getOptionList()
      },
      // 重置表单
      resetOptionForm() {
        this.optionForm = JSON.parse(JSON.stringify(this.optionFormTemp))
        this.$refs.optionFormRef.resetFields()
      },
      // 关闭表单
      closeOptionForm() {
        this.resetOptionForm()
        this.addWorkerFormVisible = false
      }
    }
  }
</script>

<style>
</style>
